package cn.uniqueinfo.dp.controller;

import cn.uniqueinfo.dp.controller.base.BaseController;
import cn.uniqueinfo.dp.exception.UQException;
import cn.uniqueinfo.dp.service.IFormButtonService;
import cn.uniqueinfo.dp.util.ResultUtil;
import cn.uniqueinfo.dp.vo.FormButtonVO;
import cn.uniqueinfo.dp.vo.Result;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-24 15:27:47
 * @description:业务表单按钮设置
 */
@Slf4j
@Controller
@RequestMapping(value = "/formbutton")
public class FormButtonController extends BaseController<FormButtonVO> {
    private IFormButtonService service;

    public FormButtonController(IFormButtonService service) {
        this.service = service;
        this.baseService = service;
        this.ctlName = "formbutton";
        this.tipName = "业务表单按钮设置";
    }

    /** 批量生成按钮 */
    @PostMapping("/bg")
    @ResponseBody
    public Result batchGenerator(@RequestParam(name = "p", required = true) String p) {
        try {
            List<FormButtonVO> list = JSONObject.parseArray(p, FormButtonVO.class);
            if (list == null || list.size() == 0) {
                return ResultUtil.fail();
            }
            this.service.deleteByFormName(list.get(0).getFormName());
            for (FormButtonVO formButtonVO : list) {
                this.service.insert(formButtonVO);
            }
            return ResultUtil.success();
        } catch (Exception e) {
            throw new UQException(0, "batchGenerator", "生成默认业务表单按钮错误", e);
        }
    }
}
